package com.example.springbooteasycode.utils.word.bad;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;

public class CreateWordDocument {

    public static void main(String[] args) throws IOException {
        createWordDocumentTableQianYi();
    }

    private static void createWordDocumentTableDouBao() throws IOException {
        try (XWPFDocument document = new XWPFDocument()) {
            // 设置标题
            XWPFParagraph titleParagraph = document.createParagraph();
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setText("第__届业主委员会委员候选人报名表");
            titleRun.setFontSize(16);
            titleRun.setBold(true);

            // 创建表格
            XWPFTable table = document.createTable(6, 6);

            // 填充表头
            setCellText(table.getRow(0).getCell(0), "姓 名");
            setCellText(table.getRow(0).getCell(1), "测试1");
            setCellText(table.getRow(0).getCell(2), "性 别");
            setCellText(table.getRow(0).getCell(3), "男");
            setCellText(table.getRow(0).getCell(4), "出生\n年月");
            setCellText(table.getRow(0).getCell(5), "1981年2月");

            setCellText(table.getRow(1).getCell(0), "职 业");
            setCellText(table.getRow(1).getCell(1), "公务员");
            setCellText(table.getRow(1).getCell(2), "文化\n程度");
            setCellText(table.getRow(1).getCell(3), "硕士");
            setCellText(table.getRow(1).getCell(4), "政治\n面貌");
            setCellText(table.getRow(1).getCell(5), "中共党员");

            setCellText(table.getRow(2).getCell(0), "专有部分座落");
            setCellText(table.getRow(2).getCell(1), "1-1-101");
            setCellText(table.getRow(2).getCell(2), "专有部分面积");
            setCellText(table.getRow(2).getCell(3), "98.00 ㎡");

            // 报名理由段落
            XWPFParagraph reasonParagraph = document.createParagraph();
            reasonParagraph.setSpacingAfter(100);
            XWPFRun reasonRun = reasonParagraph.createRun();
            reasonRun.setText("报名理由:");

            XWPFParagraph longReasonParagraph = document.createParagraph();
            XWPFRun longReasonRun = longReasonParagraph.createRun();
            longReasonRun.setText("报名理由报名理由报名理由报名理由报名理由报名理由报名理由报名理由报名理由报名理由");

            // 签名段落
            XWPFParagraph signatureParagraph = document.createParagraph();
            signatureParagraph.setAlignment(ParagraphAlignment.RIGHT);
            XWPFRun signatureRun = signatureParagraph.createRun();
            signatureRun.setText("签 名:");

            // 交表时间段落
            XWPFParagraph timeParagraph = document.createParagraph();
            timeParagraph.setAlignment(ParagraphAlignment.RIGHT);
            XWPFRun timeRun = timeParagraph.createRun();
            timeRun.setText("交表时间 2025年2月19日15时");

            // 保存文件
            try (FileOutputStream out = new FileOutputStream("/Users/boykchen/Desktop/candidate_application_form.docx")) {
                document.write(out);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private static void setCellText(XWPFTableCell cell, String text) {
        XWPFParagraph paragraph = cell.getParagraphs().isEmpty()? cell.addParagraph() : cell.getParagraphArray(0);
        XWPFRun run = paragraph.getRuns().isEmpty()? paragraph.createRun() : paragraph.getRuns().get(0);
        run.setText(text);
    }


    private static void createWordDocumentTableQianYi() throws IOException {
        // 创建一个新的Word文档
        XWPFDocument document = new XWPFDocument();

        // 添加标题
        XWPFParagraph titlePara = document.createParagraph();
        XWPFRun titleRun = titlePara.createRun();
        titleRun.setText("第_届业主委员会委员候选人报名表");
        titleRun.setFontSize(14);
        titleRun.setBold(true);
        titlePara.setAlignment(ParagraphAlignment.CENTER);

        // 创建表格
        XWPFTable table = document.createTable();
        table.setWidth("100%");

//        // 设置表格边框
//        CTBorder ctBorder = (CTBorder) table.getCTTbl().addNewTblPr().addNewTblBorders();
//        ((CTTblBorders) ctBorder).addNewTop().setVal(STBorder.THICK);
//        ((CTTblBorders) ctBorder).addNewBottom().setVal(STBorder.THICK);
//        ((CTTblBorders) ctBorder).addNewLeft().setVal(STBorder.THICK);
//        ((CTTblBorders) ctBorder).addNewRight().setVal(STBorder.THICK);
        // 表格第一行
        XWPFTableRow row1 = table.getRow(0);
        row1.getCell(0).setText("姓名");
        row1.addNewTableCell().setText("测试1");
        row1.addNewTableCell().setText("性别");
        row1.addNewTableCell().setText("男");
        row1.addNewTableCell().setText("出生年月");
        row1.addNewTableCell().setText("1981年2月");

        // 表格第二行
        XWPFTableRow row2 = table.createRow();
        row2.getCell(0).setText("职业");
        row2.addNewTableCell().setText("公务员");
        row2.addNewTableCell().setText("文化程度");
        row2.addNewTableCell().setText("硕士");
        row2.addNewTableCell().setText("政治面貌");
        row2.addNewTableCell().setText("中共党员");

        // 表格第三行
        XWPFTableRow row3 = table.createRow();
        row3.getCell(0).getCTTc().addNewTcPr().addNewGridSpan().setVal(BigInteger.valueOf(2));
        row3.getCell(0).setText("专有部分座落");
        row3.addNewTableCell().setText("1-1-101");
        row3.addNewTableCell().setText("专有部分面积");
        row3.addNewTableCell().setText("98.00㎡");

        // 表格第四行
        XWPFTableRow row4 = table.createRow();
        row4.getCell(0).getCTTc().addNewTcPr().addNewGridSpan().setVal(BigInteger.valueOf(6));
        row4.getCell(0).setText("报名理由：\n报名理由报名理由报名理由报名理由报名理由报名理由报名理由报名理由报名理由报名理由\n签名：");

        // 设置单元格宽度
        for (int i = 0; i < 6; i++) {
            row1.getCell(i).setWidth("17%");
        }

        // 将文档保存到文件系统
        String filePath = "/Users/boykchen/Desktop/报名表.docx";
        File file = new File(filePath);
        try (FileOutputStream out = new FileOutputStream(file)) {
            document.write(out);
        }

        System.out.println("文档已成功创建并保存至: " + file.getAbsolutePath());

        // 关闭文档
        document.close();
    }


    /**
     * 最简单的word文档
     * @throws IOException
     */
    private static void createWordDocumentJustSimple() throws IOException {
        // 创建一个新的Word文档
        XWPFDocument document = new XWPFDocument();

        // 创建一个新的段落
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("这是一个标题");
        run.setBold(true); // 设置为粗体
        run.setFontSize(18);

        // 添加另一个段落
        paragraph = document.createParagraph();
        run = paragraph.createRun();
        run.setText("这是文档的第一段文字。这里可以输入任何你想要的内容。");

        // 可以继续添加更多的段落或其它元素...

        // 将文档保存到文件系统
        String filePath = "/Users/boykchen/Desktop/example.docx";
        File file = new File(filePath);
        try (FileOutputStream out = new FileOutputStream(file)) {
            document.write(out);
        }

        System.out.println("文档已成功创建并保存至: " + file.getAbsolutePath());

        // 关闭文档
        document.close();
    }


}
